﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class MapDetail : DetailBasePage<DTOMap>
    {
        private string LISTDDATA = "LISTDATAMapDetail";
        private MapPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new MapPresenter();
            tblMap.Edit_Click += new EventHandler(tblMall_Edit_Click);
            tblMap.Delete_Click += new EventHandler(tblMall_Delete_Click);
            if (!IsPostBack)
            {
                PrepareView();
            }
        }

        void tblMall_Delete_Click(object sender, EventArgs e)
        {
            if (MapGrid.SelectedIndex < 0) return;
            if (_presenter.Delete(Convert.ToInt32(MapGrid.SelectedValue)))
            {
                ShowMessage(LanguageLoader.GetText("MAP_DELETE_SUCCESS"), LanguageLoader.GetText("SUCCESS"));
                PrepareView();
            }
        }

        void tblMall_Edit_Click(object sender, EventArgs e)
        {
            if (MapGrid.SelectedIndex < 0) return;
            Response.Redirect(string.Format("~/BasicView/Map.aspx?Id={0}", MapGrid.SelectedValue.ToString()));
        }

        private void PrepareView()
        {
            Session[this.LISTDDATA] = _presenter.GetMaps();
            BindGrid();
        }


        /// <summary>
        /// Bind the data grid.
        /// </summary>
        private void BindGrid()
        {
            if (Session[this.LISTDDATA] == null)
            {
                return;
            }
            this.ViewData = Session[this.LISTDDATA] as List<DTOMap>;
            MapGrid.DataSource = this.ViewData;
            MapGrid.DataBind();
        }

       #region Grid related Events
        public void MapGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Session[this.LISTDDATA] != null)
            {
                base.ViewData = Session[this.LISTDDATA] as List<DTOMap>;
                Session[this.LISTDDATA] = SortViewData(e.SortExpression);
                BindGrid();
            }
        }

        protected void MapGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            MapGrid.PageIndex = e.NewPageIndex;
            BindGrid();
            MapGrid.SelectedIndex = -1;
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResource();
        }

        private void SetLanguageAndResource()
        {
            MapGrid.EmptyDataText = LanguageLoader.GetText("NO_AVAILABLE_MAP");
            MapGrid.Columns[0].HeaderText = LanguageLoader.GetText("TITLE");
            MapGrid.Columns[1].HeaderText = LanguageLoader.GetText("SHOP_COUNT");
        }
    }
}